<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JVM老年代垃圾回收算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .card {
            transition: all 0.3s ease;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
        }
        .algorithm-icon {
            font-size: 2.5rem;
            color: #6e8efb;
            margin-bottom: 1rem;
        }
        .pros-cons-item {
            position: relative;
            padding-left: 1.5rem;
        }
        .pros-cons-item:before {
            content: "";
            position: absolute;
            left: 0;
            top: 0.5rem;
            width: 0.75rem;
            height: 0.75rem;
            border-radius: 50%;
        }
        .pros:before {
            background-color: #4ade80;
        }
        .cons:before {
            background-color: #f87171;
        }
        .highlight-box {
            position: relative;
            border-left: 4px solid #6e8efb;
            background-color: #f8fafc;
            padding: 1.5rem;
        }
        .first-letter {
            float: left;
            font-size: 3.5rem;
            line-height: 1;
            margin-right: 0.5rem;
            font-weight: 700;
            color: #6e8efb;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-4xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6">JVM老年代垃圾回收算法</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">深入解析现代JVM中的内存管理机制</p>
            <div class="w-full max-w-2xl mx-auto">
                <div class="mermaid">
                    graph LR
                    A[老年代回收算法] --> B[标记-清除]
                    A --> C[标记-整理]
                    A --> D[分代收集]
                    A --> E[G1垃圾回收器]
                    A --> F[CMS垃圾回收器]
                    style A fill:#6e8efb,color:#fff
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto max-w-4xl px-4 py-12">
        <div class="mb-16">
            <div class="first-letter">老</div>
            <p class="text-lg leading-relaxed">
                年代的清除算法主要包括标记-清除（Mark-Sweep）、标记-整理（Mark-Compact）和分代收集策略。其中，现代 JVM（如 Oracle JDK 和 OpenJDK）通常结合使用这些算法，或者采用更先进的垃圾回收器（如 G1 和 CMS）来优化老年代的垃圾回收过程。这些算法的选择直接影响着应用的性能和稳定性，理解它们的原理和适用场景对于JVM调优至关重要。
            </p>
        </div>

        <!-- Algorithm Cards -->
        <div class="grid md:grid-cols-2 gap-8 mb-16">
            <!-- Mark-Sweep -->
            <div class="card bg-white p-6">
                <div class="flex items-center mb-4">
                    <i class="fas fa-broom algorithm-icon mr-4"></i>
                    <h2 class="text-2xl font-bold">标记-清除算法</h2>
                </div>
                <h3 class="text-lg font-semibold mb-2 text-gray-700">原理</h3>
                <ul class="mb-4 space-y-2">
                    <li><strong>标记阶段</strong>：从根对象开始，标记所有存活的对象</li>
                    <li><strong>清除阶段</strong>：扫描堆内存，清除没有被标记的对象，释放它们占用的内存</li>
                </ul>
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <h4 class="font-medium mb-2 text-green-600">优点</h4>
                        <ul class="space-y-2">
                            <li class="pros-cons-item pros">实现简单</li>
                            <li class="pros-cons-item pros">适合老年代</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="font-medium mb-2 text-red-600">缺点</h4>
                        <ul class="space-y-2">
                            <li class="pros-cons-item cons">会产生内存碎片</li>
                            <li class="pros-cons-item cons">清除阶段可能耗时</li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Mark-Compact -->
            <div class="card bg-white p-6">
                <div class="flex items-center mb-4">
                    <i class="fas fa-compress-alt algorithm-icon mr-4"></i>
                    <h2 class="text-2xl font-bold">标记-整理算法</h2>
                </div>
                <h3 class="text-lg font-semibold mb-2 text-gray-700">原理</h3>
                <ul class="mb-4 space-y-2">
                    <li><strong>标记阶段</strong>：标记所有存活的对象</li>
                    <li><strong>整理阶段</strong>：将存活的对象移动到堆的一端，释放其他空间</li>
                </ul>
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <h4 class="font-medium mb-2 text-green-600">优点</h4>
                        <ul class="space-y-2">
                            <li class="pros-cons-item pros">减少内存碎片</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="font-medium mb-2 text-red-600">缺点</h4>
                        <ul class="space-y-2">
                            <li class="pros-cons-item cons">移动对象带来性能开销</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <!-- Generational Collection -->
        <div class="card bg-white p-6 mb-16">
            <div class="flex items-center mb-4">
                <i class="fas fa-layer-group algorithm-icon mr-4"></i>
                <h2 class="text-2xl font-bold">分代收集算法</h2>
            </div>
            <h3 class="text-lg font-semibold mb-2 text-gray-700">原理</h3>
            <ul class="mb-4 space-y-2">
                <li>将堆内存划分为年轻代（Young Generation）和老年代（Old Generation）</li>
                <li>年轻代中的对象经过多次垃圾回收后，若仍然存活，就会被晋升到老年代</li>
                <li>老年代的垃圾回收相对较少，主要依赖标记-清除或标记-整理算法</li>
            </ul>
            <div class="grid grid-cols-2 gap-4">
                <div>
                    <h4 class="font-medium mb-2 text-green-600">优点</h4>
                    <ul class="space-y-2">
                        <li class="pros-cons-item pros">年轻代收集频繁且迅速</li>
                        <li class="pros-cons-item pros">老年代收集频率较低，减少停顿时间</li>
                    </ul>
                </div>
                <div>
                    <h4 class="font-medium mb-2 text-red-600">缺点</h4>
                    <ul class="space-y-2">
                        <li class="pros-cons-item cons">实现更加复杂</li>
                    </ul>
                </div>
            </div>
        </div>

        <!-- Advanced Collectors -->
        <div class="grid md:grid-cols-2 gap-8 mb-16">
            <!-- G1 -->
            <div class="card bg-white p-6">
                <div class="flex items-center mb-4">
                    <i class="fas fa-project-diagram algorithm-icon mr-4"></i>
                    <h2 class="text-2xl font-bold">G1垃圾回收器</h2>
                </div>
                <h3 class="text-lg font-semibold mb-2 text-gray-700">原理</h3>
                <ul class="mb-4 space-y-2">
                    <li>将堆内存划分为多个区域（Region）</li>
                    <li>对每个区域进行独立的垃圾回收</li>
                    <li>根据当前目标和堆使用情况选择最适合回收的区域</li>
                </ul>
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <h4 class="font-medium mb-2 text-green-600">优点</h4>
                        <ul class="space-y-2">
                            <li class="pros-cons-item pros">能够控制停顿时间</li>
                            <li class="pros-cons-item pros">适合大堆内存应用</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="font-medium mb-2 text-red-600">缺点</h4>
                        <ul class="space-y-2">
                            <li class="pros-cons-item cons">配置和调优复杂</li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- CMS -->
            <div class="card bg-white p-6">
                <div class="flex items-center mb-4">
                    <i class="fas fa-tachometer-alt algorithm-icon mr-4"></i>
                    <h2 class="text-2xl font-bold">CMS垃圾回收器</h2>
                </div>
                <h3 class="text-lg font-semibold mb-2 text-gray-700">原理</h3>
                <ul class="mb-4 space-y-2">
                    <li>通过并发标记和清除进行垃圾回收</li>
                    <li>分为初始标记、并发标记、重新标记和并发清除几个阶段</li>
                </ul>
                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <h4 class="font-medium mb-2 text-green-600">优点</h4>
                        <ul class="space-y-2">
                            <li class="pros-cons-item pros">相对较低的停顿时间</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="font-medium mb-2 text-red-600">缺点</h4>
                        <ul class="space-y-2">
                            <li class="pros-cons-item cons">可能导致内存碎片</li>
                            <li class="pros-cons-item cons">并发阶段增加CPU负担</li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>

        <!-- Summary -->
        <div class="highlight-box mb-16">
            <h2 class="text-2xl font-bold mb-4">关键总结</h2>
            <div class="mermaid">
                pie
                    title 老年代垃圾回收器适用场景
                    "标记-清除" : 15
                    "标记-整理" : 25
                    "G1" : 35
                    "CMS" : 25
            </div>
            <p class="mt-4">现代JVM通常会根据应用的特性和需求选择合适的垃圾回收策略。对于需要低延迟的应用，G1和CMS是较好的选择；而对于内存占用较大的应用，标记-整理算法能有效减少碎片问题。</p>
        </div>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-4xl px-4 text-center">
            <div class="mb-4">
                <h3 class="text-xl font-medium text-white mb-2">技术小馆</h3>
                <a href="http://www.yuque.com/jtostring" class="text-blue-300 hover:text-blue-100 transition-colors">http://www.yuque.com/jtostring</a>
            </div>
            <p class="text-sm opacity-75">© 2023 JVM技术研究. 保留所有权利.</p>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>